Repository navigation
perf(header): skip the 12-slot step when a HeaderMap first grows - #886
Closed
patrick-immometrica wants to merge 1 commit into
Closed
patrick-immometrica wants to merge 1 commit into
patrick-immometrica wants to merge 1 commit into
Conversation
A map from HeaderMap::new() starts with 6 usable slots and doubles from there, so a header block of 13-24 entries, which is common for browser requests, rehashes twice: 6 -> 12 -> 24. Going straight from 8 to 32 raw slots on the first grow makes that a single rehash and saves a pair of allocations. Maps of up to 6 entries are untouched.
patrick-immometrica
marked this pull request as ready for review
November 5, 2026 11:09
Member
|
Thanks for the PR! My gut tells me to not special case in the container here, but to instead keep a WMA in a parser to use to reserve the initial amount, wherever that is needed. |
Author
|
Makes sense. h2 is where this hurts, so I moved it into h2's frame decoder as a moving average: hyperium/h2#999. Closing this one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
HeaderMap::new()doesn't allocate. The first insert reserves 8 indices (6 usable), and every grow after that doubles. A request or response with 13-24 headers, which is normal for browsers and most proxied traffic, rehashes twice on the way: 6 -> 12 -> 24. h2 decodes every header block into a freshHeaderMap::new(), so it pays this on every request.This makes the first grow go from 8 to 32 raw slots, i.e. 6 -> 24 usable. Maps of up to 6 headers are unchanged, and so is everything after 24.
I counted instructions with cachegrind (Linux,
rust:1container). The workload appends n standard headers intoHeaderMap::new()10k times; the numbers are per map, with the empty loop subtracted.Criterion wall-clock on macOS was in line with this: -21% for 16 and 20 headers. My machine was busy, so I trust the instruction counts more.
Allocations per map:
The cost is memory for maps of 7-12 headers, which now hold room for 24. I also tried starting at 16 slots, but that made small maps 20-55% slower and doubled their size, so I went with this.
I added a test that checks the capacity after the first grow. It fails on master with
left: 12, right: 24.